//
//  ST_UpperHintManager.swift
//  btvideo
//
//  Created by mac on 2020/4/3.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit

/**
 全局顶部提示
 */
class ST_UpperHintManager: NSObject {
    
    
    static let shared = ST_UpperHintManager()
    
    
    fileprivate var displaying_view:ST_UpperHintView?
    
    fileprivate override init() {
        super.init()
    }
    
    
    /**
     弹窗
     */
    func toast(model:ST_UpperHintModel) {
        
//        当前 最上层的 rootController
//        rootController
        if let currView = displaying_view{
            
            currView.bind(model: model)
            
        }else{
            
            let currView = ST_UpperHintView()
            
            currView.bind(model: model)
            
            self.displaying_view = currView
            
            currView.alpha = 0
            
            currView.transform = CGAffineTransform(translationX: 0, y: -15)
            
            
            var rootController:UIViewController = ST_BTBootController.shared
            
            while rootController.presentedViewController != nil {
                
                rootController = rootController.presentedViewController!
            }
            
            rootController.view.addSubview(currView)
            
            let topOffset = iPhoneXSeries ? 40 + 20 : 40
            
            currView.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(15)
                make.right.equalToSuperview().offset(-15)
                make.height.equalTo(ST_UpperHintView.height)
                make.top.equalToSuperview().offset(topOffset)
            }
            
            
            let animations : () -> () = {
                currView.transform = CGAffineTransform.identity
                currView.alpha = 1
            }
            
            UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: [.curveEaseInOut], animations: animations, completion:  { (f) in
                
                
                main_thread_execute(afterDelay: 3) {
                    
                    currView.removeFromSuperview()
                    
                    self.displaying_view = nil
                }
                
            })
        }
        
        
        
    }
    
}

//MARK: - View
fileprivate class ST_UpperHintView: UIView {
    
    static let height = 69.cgFloat
    
    static let width = ScreenWidth - 30
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        self.cornerRadius = 15
        self.layer.shadowColor = UIColor(red: 1, green: 0.76, blue: 0.03, alpha: 0.4).cgColor
        self.layer.shadowOffset = CGSize(width: 0, height: 2)
        self.layer.shadowOpacity = 1
        self.layer.shadowRadius = 10
        
        self.addSubview(corner_radius_view)
        corner_radius_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(44)
            make.left.equalToSuperview().offset(12)
            make.centerY.equalToSuperview()
        }
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(avatar_image_view.snp.right).offset(10)
            make.top.equalTo(avatar_image_view).offset(2)
        }
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
        }
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(click_handle))
        
        self.addGestureRecognizer(tap)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK:
    @objc fileprivate func click_handle(){
        
        guard let model = self.curr else { return  }
        
        
        BINP.update_base_info(remoteInfo: ST_BTUserInfoFromRemote.init(accid: model.accid, avatar: model.avatar, userName: model.nickName))
        
        if model.type == 0{
            
            let vc = ST_PairsCenterViewController()
            
            vc.accid = model.accid
            
            vc.love_square_operation = true
            
            let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
            
            nav.overCurrentContext = true
            
            GlobalPresent(toController: nav)
            
        }else{
            
            ST_BTBootController.shared.presentChatController(sessionId: model.accid, chatTitle: model.nickName)
            
        }
    }
    
    
//    MARK:
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.white
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        return control
    }()
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.white
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    fileprivate lazy var corner_radius_view:UIView = {
        let control = UIView()
        control.backgroundColor = .white
        control.cornerRadius = 15
        return control
    }()

    fileprivate lazy var avatar_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 22
        control.borderColor = .white
        control.borderWidth = 1
        return control
    }()

    
    fileprivate let love_color = UIColor.init(hex: 0x38CEDB)!
    
    fileprivate let love_border_color = UIColor.init(hex: 0x38CEDB, transparency: 0.4)
    
    fileprivate let love_shadow_color = UIColor(red: 0.22, green: 0.81, blue: 0.86, alpha: 0.4).cgColor
    
    fileprivate let match_color = UIColor.init(hex: 0xFEC207)!
    
    fileprivate let matchBorderColor = UIColor.init(hex: 0xFEC207, transparency: 0.4)
    
    fileprivate let matchShadowColor = UIColor(red: 1, green: 0.76, blue: 0.03, alpha: 0.4).cgColor
    
    fileprivate var curr:ST_UpperHintModel?

    
//    MARK:
    
    func bind(model:ST_UpperHintModel)  {
        
        self.curr = model
        
        
        avatar_image_view.pin_setImage(from: model.avatar.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        if model.type == 0{
            
            titleLabel.text = model.nickName + " 喜欢了你"
            content_label.text = "点击查看Ta是谁"
            
//            self.backgroundColor = love_color
            corner_radius_view.backgroundColor = love_color
            
            self.layer.shadowColor = love_shadow_color
            
        }else{
            
            titleLabel.text = model.nickName + " 与你匹配成功"
            content_label.text = "点击和Ta立即开聊"
            
            corner_radius_view.backgroundColor = match_color
            
            self.layer.shadowColor = matchShadowColor
//            self.backgroundColor = match_color
        }
    }
    
}


//MARK: - Model
struct ST_UpperHintModel {
    /**
     0,喜欢，1,匹配
     */
    var type:Int
    
    var accid:String
    
    var avatar:String
    
    var nickName:String
    
}
